package cn.springBoot.controller;

import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

import cn.springBoot.service.SchoolService;

/**
 * @author ljx
 * @description: TODO
 * @class cn.springBoot.controller.ExportPdf
 * @create date: 2021/12/6
 */

@RestController
@RequestMapping("/exportPdf")
public class ExportPdfController {

    @Autowired
    private SchoolService schoolService;

    @PostMapping("/export")
    private void exportPDF(HttpServletRequest request, HttpServletResponse response, String text) {
        Document document = new Document(PageSize.A4);
        // 左，右，上，下
        document.setMargins(10, 30, 20, 20);

        // 判断是否存在字段
        if (text.contains("${A}")) {

            text = text.replace("${A}", "刘");
        }

        try {
            response.setContentType("application/pdf");
            response.addHeader("Content-Disposition",
                "attachment;filename=" + new String(("测试" + ".pdf").getBytes(), "utf-8"));
            PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
            document.open();
            // 在下面，body中设置了style，设置了默认字体为宋体，这样导出时的html语言就默认带有了字体，汉字才会导出成功
            String content = "<html>\n" + "<head>\n" + "<meta charset=\"UTF-8\"/>\n"
                + "<meta http-equiv=\"Content-Type\" content=\"no-cache, no-store, must-revalidate\" />\n"
                + "<meta http-equiv=\"Pragma\" content=\"no-cache\"/>\n"
                + "<meta http-equiv=\"Expires\" content=\"0\"/>\n"
                + "<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,user-scalable=no\"/>\n"
                + "<style>\n" + "body{font-family:SimSun}\n" + "table{border:1px solid black }\n" +

                "</style>\n" + "</head><body style=\"font-family: 宋体, SimSun; width:90%\">" + text + "</body></html>";
            // 这里是必须要设置编码的，不然导出中文就会乱码。
            byte[] b = content.getBytes("utf-8");

            // 将字节数组包装到流中
            ByteArrayInputStream bais = new ByteArrayInputStream(b);

            XMLWorkerHelper.getInstance().parseXHtml(writer, document, bais, Charset.forName("UTF-8"));

            bais.close();
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static String getCssStr(String content) {

        String reg = "\\.([a-zA-Z0-9_]*)\\s*(\\{[^\\}]*\\})";
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(content);
        String cssStr = "";
        while (m.find()) {
            cssStr += m.group() + "  ";
        }
        System.out.println(cssStr);
        return cssStr;
    }

}
